package cs.oor;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class OrderStorage extends SQLiteOpenHelper {
    
    private static final String DATABASE_NAME = "OrderRetrievalDb";
    
    private SQLiteDatabase db;
    
    protected Context context;
    
    public OrderStorage (Context context) {
        super(context, DATABASE_NAME, null, 2);
        this.context = context;
    }
    
    public void saveToDatabase(JSONObject json) {
        db.execSQL(
            "CREATE TABLE orders IF NOT EXISTS (" +
                "orderid varchar(32) primary key,"+
                "firstname varchar(255)," +
                "lastname varchar(255)," +
                "total float" +
            ")");
        
        try {
            ContentValues values = new ContentValues();
            values.put("orderid", json.getString("orderid"));
            values.put("firstname", json.getString("firstname"));
            values.put("lastname", json.getString("lastname"));
            values.put("total", json.getDouble("total"));
            
            db.insert("orders", "", values);
            
        } catch (JSONException e) {
            Toast.makeText(this.context, e.getMessage(), 100);
        }
        
    }
    
    public void onCreate (SQLiteDatabase db) {
        this.db = db;
    }
    
    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
        
    }
}